package cn.iocoder.yudao.module.design.controller.admin.flow;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.flow.vo.*;
import cn.iocoder.yudao.module.design.controller.admin.house.vo.simpleVo.FlowSimpleRespVO;
import cn.iocoder.yudao.module.design.convert.flow.FlowConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.flow.FlowDO;
import cn.iocoder.yudao.module.design.service.flow.FlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 流程信息")
@RestController
@RequestMapping("/design/flow")
@Validated
public class FlowController {

    @Resource
    private FlowService flowService;

    @PostMapping("/create")
    @Operation(summary ="创建流程信息")
    public CommonResult<Long> createFlow(@Valid @RequestBody FlowCreateReqVO createReqVO) {
        return success(flowService.createFlow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新流程信息")
    public CommonResult<Boolean> updateFlow(@Valid @RequestBody FlowUpdateReqVO updateReqVO) {
        flowService.updateFlow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除流程信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFlow(@RequestParam("id") Long id) {
        flowService.deleteFlow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得流程信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<FlowRespVO> getFlow(@RequestParam("id") Long id) {
        FlowDO flow = flowService.getFlow(id);
        return success(FlowConvert.INSTANCE.convert(flow));
    }

    @GetMapping("/list")
    @Operation(summary ="获得流程信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<FlowRespVO>> getFlowList(@RequestParam("ids") Collection<Long> ids) {
        List<FlowDO> list = flowService.getFlowList(ids);
        return success(FlowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得流程信息分页")
    public CommonResult<PageResult<FlowRespVO>> getFlowPage(@Valid FlowPageReqVO pageVO) {
        PageResult<FlowDO> pageResult = flowService.getFlowPage(pageVO);
        return success(FlowConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/all")
    @Operation(summary ="获得流程信息分页")
    public CommonResult<List<FlowRespVO>> getFlowAll(@Valid FlowBaseVO baseVO) {
        List<FlowDO> list = flowService.getFlowAll(baseVO);
        return success(FlowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出流程信息 Excel")
    public void exportFlowExcel(@Valid FlowExportReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        List<FlowDO> list = flowService.getFlowList(exportReqVO);
        // 导出 Excel
        List<FlowExcelVO> datas = FlowConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "流程信息.xls", "数据", FlowExcelVO.class, datas);
    }
    @GetMapping("/list-all-simple")
    @Operation(summary = "获取流程精简信息列表" )
    public CommonResult<List<FlowSimpleRespVO>> getSimpleHouse() {
        // 获用户门列表，只要开启状态的
        List<FlowDO> list = flowService.getFlowByStatus(CommonStatusEnum.ENABLE.getStatus());
        // 排序后，返回给前端
        return success(FlowConvert.INSTANCE.convertList04(list));
    }


    @PostMapping("/createPurchaseFlow")
    @Operation(summary ="创建采购流程信息")
    public CommonResult<Boolean> createPurchaseFlow(@Valid @RequestBody FlowCreatePurcharseReqVO createReqVO) {
        flowService.createPurchaseFlow(createReqVO);
        return success(true);
    }

    @GetMapping("/getPurchaseFlowList")
    @Operation(summary ="查询采购流程信息")
    public CommonResult<List<Map>> getPurchaseFlow(@RequestParam("houseId") Long id) {
        List<Map>  resultList = flowService.getPurchaseFlow(id);
        return success(resultList);
    }


}
